//
//  HYBaseMenuViewController.m
//  _normal
//
//  Created by Ezra on 16/6/5.
//  Copyright © 2016年 Ezra. All rights reserved.
//

#import "HYBaseMenuViewController.h"


static NSString * const ID = @"cell";
@interface HYBaseMenuViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UICollectionView *collectionView;

@property (nonatomic, weak) UIButton *selButton;
@property (nonatomic, strong) NSMutableArray *titleButtonM;
@property (nonatomic, weak) UIView *underLineView;
@property (nonatomic, assign) BOOL isInitial;

@end

@implementation HYBaseMenuViewController
-(NSMutableArray *)titleButtonM
{
    if (!_titleButtonM) {
        
        _titleButtonM = [NSMutableArray array];
        
    }
    return _titleButtonM;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    //设置内容滚动视图
    [self setupContentView];
    
    //设置顶部titleView标题
    [self setupTopTitleView];
    
//    //设置titleView的按钮
    [self setupAllTitle];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

//在加载视图控制器之前调用 就会显示
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_isInitial == NO) {
        // 添加标题
        [self setupAllTitle];
        
        self.isInitial = YES;
    }
}
#pragma mark - 设置titleView按钮
- (void)setupAllTitle
{
    
    NSInteger count = self.childViewControllers.count;
    CGFloat buttonW = HYScreenW / count;
    CGFloat buttonH = self.titleScrollView.HY_height;
    CGFloat buttonY = 0;
    
    for (int i = 0; i < count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        
        //设置按钮标题
        UIViewController *vc = self.childViewControllers[i];
        [button setTitle:vc.title forState:UIControlStateNormal];
        
        //设置文字颜色
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        UIColor *color = PrimaryYellow;
        [button setTitleColor:color  forState:UIControlStateSelected];
        
        //设置文字大小，按钮尺寸
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.frame = CGRectMake(buttonW * i,buttonY ,buttonW ,buttonH);
        
        //监听按钮点击
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.titleButtonM addObject:button];
        
        [self.titleScrollView addSubview:button];
        
        if (i == 0) {
            
            //按钮红色指示器
            CGFloat lineH = 1;
            CGFloat lineY = self.titleScrollView.HY_height - lineH;
            
            UIView *underLineView = [[UIView alloc]init];
            //注意 先计算文字尺寸，再去给label赋值
            [button.titleLabel sizeToFit];
            
            underLineView.HY_height = lineH;
            underLineView.HY_width = button.titleLabel.HY_width;
            underLineView.HY_y = lineY;
            underLineView.HY_centerX = button.HY_centerX;
            
            underLineView.backgroundColor = PrimaryYellow;
            
            self.underLineView = underLineView;
            
            [self.titleScrollView addSubview:underLineView];
            
            //默认第一个按钮点击
            [self titleClick:button];
        }

    }

}

#pragma mark - 监听标题按钮点击
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    //双击刷新界面
    if (button == self.selButton) {
        UITableViewController *vc = self.childViewControllers[i];
        
        [vc refreshControl];
    }
    
    [self setSelTitelButton:button];
    CGFloat offsetX = i * HYScreenW;
    
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
/**按钮点击变色的方法*/
- (void)setSelTitelButton:(UIButton *)button
{
    self.selButton.selected = NO;
    button.selected = YES;
    self.selButton = button;
  
    [UIView animateWithDuration:0.25 animations:^{
        
        self.underLineView.HY_centerX = button.HY_centerX;
        
    }];
}


#pragma mark - 设置顶部title视图
- (void)setupTopTitleView
{
    CGFloat titleViewH = 35;
    
    UIScrollView *titleView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, HYScreenW, titleViewH)];
    
    //取消状态栏滚动
    titleView.scrollsToTop = NO;
    
    titleView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    self.titleScrollView = titleView;
    [self.view addSubview:titleView];
    
}

#pragma mark - 设置内容滚动视图
- (void)setupContentView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(HYScreenW, HYScreenH);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //尺寸用self.view.bounds
    UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.titleScrollView.HY_maxY, HYScreenW, HYScreenH)  collectionViewLayout:flowLayout];
    
    collectView.scrollsToTop = NO;
    
    collectView.delegate = self;
    collectView.dataSource = self;
    collectView.showsHorizontalScrollIndicator = NO;
    collectView.pagingEnabled = YES;
    collectView.bounces = NO;
    
    self.collectionView = collectView;
    [self.view addSubview:collectView];
    
    [collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / HYScreenW;
    
//    HYBaseThemeViewController *vc = self.childViewControllers[i];
//    [vc reloadData];
    
    UIButton *selButton = self.titleButtonM[i];
    
    [self setSelTitelButton:selButton];
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    //移除之前的自控制器
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //把子控制器的View添加到cell上
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

@end
